package com.css.zfzx.fwsjcj.modules.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.org.dept.repository.entity.DeptEntity;
import com.css.bpm.platform.org.user.repository.entity.DeptUserEntity;
import com.css.bpm.platform.org.user.repository.entity.UserEntity;
import com.css.bpm.platform.page.login.viewobjects.PwdCheck;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.bpm.platform.utils.UUIDGenerator;
import com.css.zfzx.fwsjcj.commen.util.Constant;
import com.css.zfzx.fwsjcj.modules.basecontroller.service.FwDivisionService;
import com.css.zfzx.fwsjcj.modules.user.entity.FwUserEntity;
import com.css.zfzx.fwsjcj.modules.user.repository.BaseDeptRepository;
import com.css.zfzx.fwsjcj.modules.user.repository.BaseUserRepository;
import com.css.zfzx.fwsjcj.modules.user.repository.FwQueryRespository;
import com.css.zfzx.fwsjcj.modules.user.repository.FwUserRepository;
import com.css.zfzx.fwsjcj.modules.user.service.FwUserService;
import com.css.zfzx.fwsjcj.modules.user.vo.UserQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@PropertySource(value = "classpath:platform-config.yml")
public class FwUserServiceImpl implements FwUserService {
    @Autowired
    private FwUserRepository fwUserRepository;
    @Autowired
    private FwQueryRespository fwQueryRespository;
    @Autowired
    private BaseUserRepository baseUserRepository;
    @Autowired
    private BaseDeptRepository baseDeptRepository;
    @Autowired
    private FwDivisionService fwDivisionService;

    private FwUserService fwUserService;
    @Override
    public void saveUserApp(FwUserEntity fwUserEntity) {
        fwUserEntity.setId(UUIDGenerator.getUUID());
        fwUserEntity.setStatus(Constant.STATUS_DAI_SH);
        fwUserEntity.setIsValid(Constant.IS_VALID_YES);
        if(StringUtils.isEmpty(fwUserEntity.getUserType())) {
            fwUserEntity.setUserType(Constant.USER_TYPE_PC);
        }
        //fwUserEntity.setDivision(Contant.USER_TYPE_PC);
        fwUserEntity.setMobilePhone(fwUserEntity.getUserName());
        fwUserEntity.setCreateTime(new Date());
        fwUserRepository.save(fwUserEntity);
    }

    @Override
    public void saveUser(FwUserEntity fwUserEntity) {
        fwUserEntity.setId(UUIDGenerator.getUUID());
        fwUserEntity.setStatus(Constant.STATUS_DAI_SH);
        fwUserEntity.setIsValid(Constant.IS_VALID_YES);
        String fullNameById = fwDivisionService.findFullNameById(fwUserEntity.getDivision());
        fwUserEntity.setUserAddress(fullNameById);
        //fwUserEntity.setDivision(Contant.USER_TYPE_XC);
        fwUserEntity.setMobilePhone(fwUserEntity.getUserName());
        fwUserEntity.setCreateTime(new Date());
        fwUserRepository.save(fwUserEntity);
    }

    @Override
    public JSONObject getPage(HttpServletRequest request,UserQueryVo userQueryVo, int curPage, int pageSize) {
        Page<FwUserEntity> page=fwQueryRespository.queryUser(request,userQueryVo,curPage,pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(page);
        return jsonObject;
    }

    @Override
    public FwUserEntity findByUserId(String userId) {
        FwUserEntity fwUserEntity= fwUserRepository.findById(userId).get();
        return fwUserEntity;
    }

    @Override
    public void deletUser(List<String> ids) {
        Date updateTime=new Date();
        List<String> baseId=new ArrayList<>();
        for(String id:ids){
            FwUserEntity userEntity = fwUserRepository.findById(id).get();
            String baseUserId = userEntity.getBaseUserId();
            UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(baseUserId);
            if(!StringUtils.isEmpty(user)){
                baseId.add(baseUserId);
            }
        }
        PlatformAPI.getOrgAPI().getUserAPI().deleteUsers(baseId);
        fwUserRepository.deleteByIds(ids,updateTime);
    }

    @Override
    public PwdCheck doLogin(String userName,String password,String privateKey) throws Exception {
       PwdCheck pwdCheck=PlatformAPI.getOrgAPI().getUserAPI().checkLoginPassword(userName,password,privateKey);
        return pwdCheck;
    }

    @Override
    @Transactional
    public void updateType(String type, List<String> ids) throws Exception {
        if(type.equals(Constant.STATUS_SUCESS_SH)){
            for (String id:ids) {
                FwUserEntity fwUserEntity = fwUserRepository.findById(id).get();
                String division = fwUserEntity.getDivision();
                UserEntity userEntity=new UserEntity();
                userEntity.setUserName(fwUserEntity.getRelName());
                userEntity.setIsValid(fwUserEntity.getIsValid());
                userEntity.setMobilePhone(fwUserEntity.getUserName());
                userEntity.setUserCode(fwUserEntity.getUserName());
                userEntity.setIdCard(fwUserEntity.getIdNumber().toString());
                userEntity.setExtend1(fwUserEntity.getUserType());
                userEntity.setCreateTime(new Date());
                //userEntity.setCreateUser(fwUserEntity.getCreateUser());
                UserEntity user= PlatformAPI.getOrgAPI().getUserAPI().saveUser(userEntity);
                String baseUserId=user.getUserId();
                PlatformAPI.getOrgAPI().getUserAPI().saveCertification(baseUserId,fwUserEntity.getPwd());
                List<String> list=new ArrayList<>();
                list.add(baseUserId);
                PlatformAPI.getOrgAPI().getUserAPI().activeUsers(list);
                //如果为普查采集员将用户添加到信息采集机构
                if(Constant.USER_TYPE_PC.equals(fwUserEntity.getUserType())) {
                    DeptEntity deptEntity = baseDeptRepository.findByDivisionIdAndExtend1(division, Constant.ORG_WAI);
                    String deptId = deptEntity.getDeptId();
                    DeptUserEntity deptUserEntity = new DeptUserEntity();
                    deptUserEntity.setDeptId(deptId);
                    deptUserEntity.setUserId(baseUserId);
                    deptUserEntity.setIsMain(Constant.IS_MAIN_YES);
                    PlatformAPI.getOrgAPI().getDeptAPI().saveDeptUser(deptUserEntity);
                }else if (Constant.USER_TYPE_JG.equals(fwUserEntity.getUserType())){
                    //如果为加固工程采集员添加到加固工程信息采集机构
                    DeptEntity deptEntity = baseDeptRepository.findByDivisionIdAndExtend1(division, Constant.ORG_JG);
                    String deptId = deptEntity.getDeptId();
                    DeptUserEntity deptUserEntity = new DeptUserEntity();
                    deptUserEntity.setDeptId(deptId);
                    deptUserEntity.setUserId(baseUserId);
                    deptUserEntity.setIsMain(Constant.IS_MAIN_YES);
                    PlatformAPI.getOrgAPI().getDeptAPI().saveDeptUser(deptUserEntity);
                }else if (Constant.USER_TYPE_JG_ANG_PC.equals(fwUserEntity.getUserType())){
                    //既是信息采集员又是加固工程采集员
                    DeptEntity deptEntity = baseDeptRepository.findByDivisionIdAndExtend1(division, Constant.ORG_WAI);
                    String deptId = deptEntity.getDeptId();
                    DeptUserEntity deptUserEntity = new DeptUserEntity();
                    deptUserEntity.setDeptId(deptId);
                    deptUserEntity.setUserId(baseUserId);
                    deptUserEntity.setIsMain(Constant.IS_MAIN_YES);
                    PlatformAPI.getOrgAPI().getDeptAPI().saveDeptUser(deptUserEntity);
                    //添加到加固工程信息采集机构
                    DeptEntity deptEntityJG = baseDeptRepository.findByDivisionIdAndExtend1(division, Constant.ORG_JG);
                    String deptIdJG = deptEntityJG.getDeptId();
                    DeptUserEntity deptUserEntityJG = new DeptUserEntity();
                    deptUserEntityJG.setDeptId(deptIdJG);
                    deptUserEntityJG.setUserId(baseUserId);
                    deptUserEntityJG.setIsMain(Constant.IS_MAIN_NO);
                    PlatformAPI.getOrgAPI().getDeptAPI().saveDeptUser(deptUserEntityJG);
                }else if(Constant.USER_TYPE_XC.equals(fwUserEntity.getUserType())){
                    DeptEntity deptEntity = baseDeptRepository.findByDivisionIdAndExtend1(division, Constant.ORG_WAI);
                    String deptId = deptEntity.getDeptId();
                    DeptUserEntity deptUserEntity = new DeptUserEntity();
                    deptUserEntity.setDeptId(deptId);
                    deptUserEntity.setUserId(baseUserId);
                    deptUserEntity.setIsMain(Constant.IS_MAIN_YES);
                    PlatformAPI.getOrgAPI().getDeptAPI().saveDeptUser(deptUserEntity);
                }
//                DeptUserEntity deptUserEntity=new DeptUserEntity();
//                deptUserEntity.setDeptId(fwUserEntity.getOrgId());
//                deptUserEntity.setUserId(baseUserId);
//                PlatformAPI.getOrgAPI().getDeptAPI().saveDeptUser(deptUserEntity);
                fwUserRepository.updateById(id,baseUserId);
            }
            fwUserRepository.updateType(ids,type);
        }else{
            fwUserRepository.updateType(ids,type);
        }
    }

    @Override
    public void updateUser(UserEntity userEntity) {

    }

    @Override
    public List<UserEntity> getAppUser(String type) {
        List<UserEntity> userByType = baseUserRepository.findUserByType(type);
        return userByType;
    }

    @Override
    public FwUserEntity findByUserName(String userName) {
        FwUserEntity byUserName = fwUserRepository.findByUserName(userName);
        return byUserName;
    }

    @Override
    public FwUserEntity findByIdNumber(String idNumber) {
        FwUserEntity byIdNumber = fwUserRepository.findByIdNumber(idNumber);
        return byIdNumber;
    }

    @Override
    public FwUserEntity findByMobilePhon(String idNumber) {
        FwUserEntity byMobilePhone = fwUserRepository.findByMobilePhone(idNumber);
        return byMobilePhone;
    }

    @Override
    public FwUserEntity findByBaseUserId(String baseUserId) {
        FwUserEntity fwUserEntity = fwUserRepository.findByBaseUserId(baseUserId);
        return fwUserEntity;
    }

    @Override
    public void deleteFwUser(List<String> ids) {
        fwUserRepository.deleteBatch(ids);
    }
}
